import { Injectable } from "@nestjs/common";
import axios from "axios";
import * as fs from "fs";
import * as path from "path";

const cacheFilePath = path.join("./public/jsons", "cacheGeo.json"); // 本地缓存文件路径

@Injectable()
export class ChartsService {
  async getGeoJSONData() {
    if (fs.existsSync(cacheFilePath)) {
      // 如果本地缓存文件存在，则读取缓存数据
      const cacheData = fs.readFileSync(cacheFilePath, "utf-8");
      return JSON.parse(cacheData);
    }
    const url = "https://geojson.cn/api/data/china.json"; // 替换为实际的 API URL
    try {
      const response = await axios.get(url);
      const data = response.data;
      fs.writeFileSync(cacheFilePath, JSON.stringify(data)); // 将数据写入本地缓存文件
      return data;
    } catch (error) {
      throw Error(error);
    }
  }
  getChinaData() {
    return {
      chinaGeoCoordMap: {
        四川: [103.9526, 30.7617],
        黑龙江: [127.9688, 45.368],
        内蒙古: [110.3467, 41.4899],
        吉林: [125.8154, 44.2584],
        北京市: [116.4551, 40.2539],
        辽宁: [123.1238, 42.1216],
        河北: [114.4995, 38.1006],
        天津: [117.4219, 39.4189],
        山西: [112.3352, 37.9413],
        陕西: [109.1162, 34.2004],
        甘肃: [103.5901, 36.3043],
        宁夏: [106.3586, 38.1775],
        青海: [101.4038, 36.8207],
        新疆: [87.611053, 43.828171],
        西藏: [91.117212, 29.646922],
        重庆: [108.384366, 30.439702],
        山东: [117.1582, 36.8701],
        河南: [113.4668, 34.6234],
        江苏: [118.8062, 31.9208],
        安徽: [117.29, 32.0581],
        湖北: [114.3896, 30.6628],
        浙江: [119.5313, 29.8773],
        福建: [119.4543, 25.9222],
        江西: [116.0046, 28.6633],
        湖南: [113.0823, 28.2568],
        贵州: [106.6992, 26.7682],
        云南: [102.9199, 25.4663],
        广东: [113.12244, 23.009505],
        广西: [108.479, 23.1152],
        海南: [110.3893, 19.8516],
        台湾: [120.702967, 24.123621],
        上海: [121.4648, 31.2891],
      },
      chinaDatas: [
        [{ name: "北京市", value: 0 }, { name: "黑龙江", value: 0 }, { name: "上海市" }],
        [{ name: "内蒙古", value: 0 }],
        [{ name: "吉林", value: 0 }],
        [{ name: "辽宁", value: 0 }],
        [{ name: "河北", value: 0 }],
        [{ name: "天津", value: 0 }],
        [{ name: "山西", value: 0 }],
        [{ name: "陕西", value: 0 }],
        [{ name: "甘肃", value: 0 }],
        [{ name: "新疆", value: 0 }],
        [{ name: "西藏", value: 0 }],
        [{ name: "台湾", value: 0 }],
        [{ name: "黑龙江", value: 0 }],
        [{ name: "云南", value: 1 }],
        [{ name: "宁夏", value: 0 }],
        [{ name: "青海", value: 0 }],
        [{ name: "四川", value: 2 }],
        [{ name: "重庆", value: 1 }],
        [{ name: "山东", value: 0 }],
        [{ name: "河南", value: 0 }],
        [{ name: "江苏", value: 0 }],
        [{ name: "安徽", value: 0 }],
        [{ name: "湖北", value: 0 }],
        [{ name: "浙江", value: 0 }],
        [{ name: "福建", value: 0 }],
        [{ name: "江西", value: 0 }],
        [{ name: "湖南", value: 0 }],
        [{ name: "贵州", value: 1 }],
        [{ name: "广西", value: 0 }],
        [{ name: "海南", value: 0 }],
        [{ name: "上海", value: 0 }],
      ],
      // 攻击起始点
      toCoords: [
        [103.9526, 30.7617],
        [103.9526, 30.7617],
        [108.384366, 30.439702],
        [108.384366, 30.439702],
        [108.384366, 30.439702],
        [108.384366, 30.439702],
        [108.384366, 30.439702],
        [102.9199, 25.4663],
        [102.9199, 25.4663],
        [102.9199, 25.4663],
        [102.9199, 25.4663],
        [102.9199, 25.4663],
        [106.6992, 26.7682],
        [106.6992, 26.7682],
        [106.6992, 26.7682],
        [106.6992, 26.7682],
        [106.6992, 26.7682],
        [103.9526, 30.7617],
        [103.9526, 30.7617],
        [103.9526, 30.7617],
        [103.9526, 30.7617],
        [103.9526, 30.7617],
        [103.9526, 30.7617],
        [103.9526, 30.7617],
        [103.9526, 30.7617],
        [103.9526, 30.7617],
        [103.9526, 30.7617],
        [103.9526, 30.7617],
      ],
    };
  }
  getAllChartData() {
    // 生成 min-max 的随机数
    function getRandomNumber(min = 0, max = 300) {
      if (max < min) throw Error("min不能比max大");
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    // 生成最近八小时的时分
    function getRecentEightHours() {
      const now = new Date(); // 获取当前时间
      const result = []; // 存储结果的数组
      for (let i = 0; i < 8; i++) {
        const hour = now.getHours() - i; // 获取当前小时数减去i
        const minute = now.getMinutes(); // 获取当前分钟数
        result.push(`${hour.toString().padStart(2, "0")}:${minute.toString().padStart(2, "0")}`); // 将时分格式化为字符串并添加到结果数组中
      }
      return result; // 返回结果数组
    }
    const mockDatas = {
      cardData: [
        {
          title: "总下单用户数量",
          num: getRandomNumber(1000, 5000),
          price: getRandomNumber(10000, 50000),
        },
        {
          title: "服务总展数量",
          num: getRandomNumber(1000, 5000),
          price: getRandomNumber(10000, 50000),
        },
        {
          title: "阿西总展数量",
          num: getRandomNumber(1000, 5000),
          price: getRandomNumber(10000, 50000),
        },
        {
          title: "现场操作订单数",
          num: getRandomNumber(1000, 5000),
          price: getRandomNumber(10000, 50000),
        },
        {
          title: "办证订单数",
          num: getRandomNumber(1000, 5000),
          price: getRandomNumber(10000, 50000),
        },
        {
          title: "回城运输订单数",
          num: getRandomNumber(1000, 5000),
          price: getRandomNumber(10000, 50000),
        },
        {
          title: "一站式运输订单数",
          num: getRandomNumber(1000, 5000),
          price: getRandomNumber(10000, 50000),
        },
      ],
      barData: [
        { name: "华北", value: getRandomNumber(200, 1000) },
        { name: "华东", value: getRandomNumber(400, 1000) },
        { name: "华南", value: getRandomNumber(300, 1000) },
        { name: "华中", value: getRandomNumber(600, 1000) },
        { name: "西北", value: getRandomNumber(100, 1000) },
        { name: "西南", value: getRandomNumber(500, 1000) },
        { name: "东北", value: getRandomNumber(200, 1000) },
      ],
      pictorialBarData: [
        { name: "2021", value: getRandomNumber(10000, 50000) },
        { name: "2022", value: getRandomNumber(15000, 50000) },
        { name: "2023", value: getRandomNumber(20000, 50000) },
        { name: "2024", value: getRandomNumber(30000, 50000) },
      ],
      lineData: {
        times: getRecentEightHours(),
        types: ["当归", "人参", "党参", "鹿茸", "雪见", "景天", "重楼", "飞蓬"],
        incomes: getRecentEightHours().map((_, index) => {
          return getRecentEightHours().map(() => {
            return Math.floor(getRandomNumber(10 * (index + 1 + Math.random() * 10), 300));
          });
        }),
      },
      wordcloudData: [
        {
          value: getRandomNumber(40, 50),
          name: "人参果",
          textStyle: {
            shadowBlur: 4,
            shadowColor: "tomato",
            shadowOffsetY: 14,
            color: "tomato",
          },
        },
        { value: getRandomNumber(12, 40), name: "党参" },
        { value: getRandomNumber(12, 40), name: "景天" },
        { value: getRandomNumber(12, 40), name: "当归" },
        { value: getRandomNumber(12, 40), name: "鹿茸" },
        { value: getRandomNumber(12, 40), name: "雪见" },
        { value: getRandomNumber(12, 40), name: "重楼" },
        { value: getRandomNumber(12, 40), name: "飞蓬" },
        { value: getRandomNumber(12, 40), name: "黄芪" },
        { value: getRandomNumber(12, 40), name: "甘草" },
        { value: getRandomNumber(12, 40), name: "茯苓" },
        { value: getRandomNumber(12, 40), name: "五味子" },
        { value: getRandomNumber(12, 40), name: "复方丹参片" },
        { value: getRandomNumber(12, 40), name: "白芷舒筋止痛膏 " },
        { value: getRandomNumber(12, 40), name: "云南白药" },
        { value: getRandomNumber(12, 40), name: "六味地黄丸" },
        { value: getRandomNumber(12, 40), name: "和香正气水" },
      ],
    };
    return mockDatas;
  }
}
